package com.arqui.admmon_module.server.services;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.xml.rpc.ServiceException;

import com.arqui.admmon_module.server.webservices.eventsmonitor.ExternalService;
import com.arqui.admmon_module.server.webservices.eventsmonitor.NonexistentEntityException;
import com.arqui.admmon_module.server.webservices.eventsmonitor.UnknownHostException;
import com.arqui.admmon_module.server.webservices.eventsmonitor.WebServices_PortType;
import com.arqui.admmon_module.server.webservices.eventsmonitor.WebServices_ServiceLocator;
import com.arqui.admmon_module.shared.ServicesEntity;

public class ExternalEventsService {

	//To find external services in historical mode
	public List<ServicesEntity> getExternaServicesHistoric(Date initDate,
			Date finalDate) throws UnknownHostException,
			NonexistentEntityException, RemoteException, ServiceException,
			Exception {
		Calendar date1 = Calendar.getInstance();
		Calendar date2 = Calendar.getInstance();
		date1.setTime(initDate);
		date2.setTime(finalDate);
		String initialDate = dateToString(date1);
		String endDate = dateToString(date2);
		 
		return mapServices(getExternalServices(initialDate, endDate));
	}

	//To find external services in real time mode
	public List<ServicesEntity> getExternaServicesRealTime()
			throws UnknownHostException, NonexistentEntityException,
			RemoteException, ServiceException, Exception {
		Calendar finalDate = Calendar.getInstance();
		Calendar initDate = finalDate;
		initDate.set(Calendar.SECOND, finalDate.get(Calendar.SECOND) - 5);

		String initialDate = dateToString(initDate);
		String endDate = dateToString(finalDate);

		return mapServices(getExternalServices(initialDate, endDate));
	}

	private ExternalService[] getExternalServices(String initDate,
			String endDate) throws ServiceException, Exception,
			UnknownHostException, NonexistentEntityException, RemoteException {
		WebServices_ServiceLocator service = new WebServices_ServiceLocator();
		WebServices_PortType port = service.getWebServicesPort();
		return port.getExternalEvents(initDate, endDate);
	}

	private String dateToString(Calendar date) {
		String stringDate = "" + date.get(Calendar.YEAR) + "-"
				+ date.get(Calendar.MONTH) + "-"
				+ date.get(Calendar.DAY_OF_MONTH) + "-"
				+ date.get(Calendar.HOUR_OF_DAY) + "-"
				+ date.get(Calendar.MINUTE) + "-" + date.get(Calendar.SECOND);
		return stringDate;
	}
	
	private List<ServicesEntity> mapServices(ExternalService[] list){
		List<ServicesEntity> finalList = new ArrayList<ServicesEntity>();
		for(ExternalService e:list){
			ServicesEntity temp = new ServicesEntity();
			temp.setDate(e.getDate());
			temp.setIpMachine(e.getIpMachine());
			temp.setNameMachine(e.getNameMachine());
			temp.setService(e.getService());
			finalList.add(temp);
		}
		return finalList;
	}

}
